from django import forms
from django.urls import reverse
from django.utils.safestring import mark_safe

from stark.service.v1 import StarkHandler,get_datetime_text,get_manytomany,BootStrapModelForm,SearchOption
from stark.forms.widgets import DateTimePickerInput
from web.models import ClassList

class ClassListModelForm(BootStrapModelForm):
    class Meta:
        model=ClassList
        fields="__all__"
        widgets={
            'start_date':DateTimePickerInput,
            'graduate_date':DateTimePickerInput
        }


class ClassListHandler(StarkHandler):
    # def get_urls(self):
    #     patterns = []
    #     # print(self.get_add_url_name, self.get_list_url_name, self.get_edit_url_name, self.get_del_url_name)
    #     patterns.append(path('', self.list_view, name="{}_list".format(self.model_name)))
    #     patterns.append(path('add/', self.add_view, name="{}_add".format(self.model_name)))
    #     return patterns
    def display_course(self,obj=None,is_header=None ,*args, **kwargs):
        if is_header:
            return '班级'
        return "{} {} 期".format(obj.course,obj.semester)

    def display_course_record(self,obj=None,is_header=None,*args, **kwargs):
        if is_header:
            return '上课记录'
        record_url=reverse('stark:web_courserecord_list',kwargs={'class_id':obj.pk})
        tpl=mark_safe("<a target='_blank' href='{}'>上课记录</a>".format(record_url))
        return tpl


    list_display = ['school',display_course,'tutor',get_datetime_text('开班日期','start_date'),
                    get_manytomany('任课老师','teachers'),display_course_record]
    order_list = ['id']
    # search_list = ['title','address']
    model_form_class = ClassListModelForm
    # search_group = [
    #     SearchOption(field='gender'),
    #     SearchOption(field='depart', db_condition={})
    # ]
    # search_list = ['school',]
    search_group = {
        SearchOption('school'),
        SearchOption('course')
    }
